package cn.wolfcode.rbac.service.Impl;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.mapper.EmployeeMapper;
import cn.wolfcode.rbac.service.LoginService;
import cn.wolfcode.rbac.util.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiahuihui
 * #Description LoginServiceImpl
 * #Date: 2022/7/1 15:57
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String username, String password, String verifycode, HttpServletRequest request) {
        boolean verifyCode = VerifyCodeUtil.verification(request,verifycode,true);

        if(!verifyCode){
            throw new RuntimeException("验证码错误");
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Employee employee = employeeMapper.login(username, password);
        if(employee==null){
            throw new RuntimeException("用户名或密码错误");
        }
        return employee;
    }
}
